package com.qf.sy2103.ssm.controller;

import com.qf.sy2103.ssm.constants.Constants;
import com.qf.sy2103.ssm.entity.Canshu;
import com.qf.sy2103.ssm.entity.Gongneng;
import com.qf.sy2103.ssm.service.CanshuService;
import com.qf.sy2103.ssm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/canshu")
public class CanshuController {
    @Autowired
    private CanshuService canshuService;
    @RequestMapping("/all")
    @ResponseBody
    public TableVo<Canshu> canshuall(PageVo pageVo, String name, Integer state){
        Canshu canshu=new Canshu();
        canshu.setName(name);
        canshu.setState(state);
        TableVo<Canshu> all = canshuService.all(pageVo, canshu);
        return all;
    }
    @RequestMapping("/del")
    @ResponseBody
    public MessageVo delete(Integer[] ids){
        int del = canshuService.del(ids);
        if (del>0){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETESUCCESS, Constants.USER_MSG_DELETESUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_DELETEFAIL, Constants.USER_MSG_DELETEFAIL, null);
        return messageVo;
    }
    @RequestMapping("/add")
    @ResponseBody
    public MessageVo kehuAdd(CanshuVo canshuVo){
        Canshu canshu=new Canshu(canshuVo.getName(),canshuVo.getDescription(),canshuVo.getState());
        int add = canshuService.add(canshu);
        if (add==1){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDSUCCESS, Constants.USER_MSG_ADDSUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_ADDFAIL, Constants.USER_MSG_ADDFAIL, null);
        return messageVo;
    }
    @RequestMapping("/update")
    @ResponseBody
    public MessageVo kehuEdit(CanshuVo canshuVo){
        Canshu canshu=new Canshu(canshuVo.getName(),canshuVo.getDescription(),canshuVo.getState());
        canshu.setId(canshuVo.getId());
        int edit = canshuService.deit(canshu);
        if (edit==1){
            MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITSUCCESS, Constants.USER_MSG_EDITSUCCESS, null);
            return messageVo;
        }
        MessageVo messageVo = new MessageVo(Constants.USER_CODE_EDITFAIL, Constants.USER_MSG_EDITFAIL, null);
        return messageVo;
    }
}
